home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / util / cli / csv2html.lha / Csv2Html / csv2html.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-19  |  6.7 KB  |  296 lines

  1. /*-Header---------------------------------------------------------------------
  2. CSV2HTML - Converts CSV files to HTML tables.
  3. Copyright (C) 1999 Jonathan Combe
  4. Ported to AmigaOS by Chris De Maeyer
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the Licence, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  14. GNU General Publi License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. The license file is in the file LICENSE supplied in the archive.
  21.  
  22. See the file INSTALL for more information on any issues of compilation
  23. and installation into a UNIX system.
  24.  
  25. This is the source code for version 1.0
  26.  
  27. Jonathan Combe 26/09/1999
  28. email : joncombe@yahoo.com
  29.  
  30.  
  31. Chris De Maeyer 18/10/1999
  32. email : ceppe@pandora.be
  33. */
  34.  
  35. /* Uncomment this if you are using a compiler such as CC under IRIX, which
  36.    doesn't support BOOL's.
  37. */
  38.  
  39. enum BOOL {FALSE=0,TRUE=1};
  40.  
  41. /* Include files */
  42.  
  43. #include <iostream.h>
  44. #include <string.h>
  45. #include <fstream.h>
  46.  
  47. /* Global Variables */
  48.  
  49. char version[100]; /* Version string */
  50.  
  51. /*-Process Files function----------------------------------------------------- 
  52.  
  53.    Version 1.0
  54.  
  55.    Processes the files and writes html file
  56.  
  57.    Returns: - 
  58.  
  59.    0 for OK. 
  60.    1 for invalid csv file
  61.    2 for invalid HTML output file
  62.    3 for process error
  63. */
  64.  
  65. int ProcessFiles(char* csvfile, char* htmlfile)
  66. {
  67.  
  68.   /* Declare file streams */
  69.  
  70.   ifstream csv;  // Input CSV File
  71.   ofstream html; // Output HTML file
  72.   char read; // For reading in the file charachter by charachter
  73.  
  74.   BOOL trflag=FALSE; // Flag for weather to write TR or not
  75.   BOOL tdflag=FALSE; // Says whether to create new TD or not
  76.   BOOL newlineflag=FALSE; // Set to TRUE when new line
  77.   BOOL quoteflag=FALSE; // Stroes wheteher we are in quotes or not
  78.  
  79.   /* Open CSV File */
  80.  
  81.   csv.open(csvfile,ios::in);
  82.  
  83.   if (!csv.good()) /* File failed to open */
  84.   {
  85.     return 1; /* Return failure */
  86.   }
  87.  
  88.   /* CSV File opened OK so open output file */ 
  89.  
  90.   html.open(htmlfile,ios::out);
  91.  
  92.   if (!html.good()) /* File open failed */
  93.   {
  94.     return 2; /* Failed */
  95.   }
  96.  
  97.   /* Both files opened OK so we can proceed! */
  98.  
  99.   /* Initialise HTML File */
  100.  
  101.   html << "<HTML>\n";
  102.   html << "<TABLE BORDER=1>\n";
  103.   
  104.   /* Start main loop */
  105.  
  106.   html << "<TR>\n<TD>";
  107.   trflag=FALSE;
  108.  
  109.   while (!csv.eof())
  110.   {
  111.     csv.get(read); /* Read in charachter */
  112.     
  113.     /* If we are starting a quote this sets to ignore any commas until
  114.        the quote is closed. This is beacuse if a .CSV file is generated
  115.        from data containg a comma the data will be put in quotes. This
  116.        flag is also to stop the quote being written into the HTML file.
  117.     */
  118.  
  119.     if (read=='"')
  120.     {
  121.       if (quoteflag==FALSE)
  122.       {
  123.         quoteflag=TRUE;
  124.       }
  125.       else
  126.       {
  127.         quoteflag=FALSE;
  128.       }
  129.     }
  130.  
  131.     if (read=='\n') /* Special Case */
  132.     {
  133.       if (tdflag==TRUE)
  134.       {
  135.         html << "<TD>";
  136.         tdflag=FALSE;
  137.       }
  138.       trflag=TRUE;
  139.       newlineflag=TRUE;
  140.     }
  141.     else if ((read==',') && (quoteflag==FALSE))
  142.     {
  143.       if (newlineflag==TRUE)
  144.       {
  145.         html << "</TD>\n</TR>";
  146.         newlineflag=FALSE;
  147.       }
  148.       if ((tdflag==TRUE) && (trflag==FALSE))
  149.       {
  150.         html << "<TD>";
  151.         tdflag=FALSE;
  152.       }
  153.       if (trflag==TRUE)
  154.       {
  155.         html << "\n<TR>\n<TD>";
  156.         trflag=FALSE;
  157.       }
  158.       html << "</TD>";
  159.       tdflag=TRUE;
  160.     }
  161.     else /* Normal Charachter */
  162.     {
  163.       if (newlineflag==TRUE)
  164.       {
  165.         html << "</TD>\n</TR>";
  166.         newlineflag=FALSE;
  167.       }
  168.       if ((trflag==FALSE) && (tdflag==TRUE))
  169.       {
  170.         html << "<TD>";
  171.         tdflag=FALSE;
  172.       }
  173.       if (trflag==TRUE)
  174.       {
  175.         html << "\n<TR>\n<TD>";
  176.         trflag=FALSE;
  177.       }
  178.       if (read!='"')
  179.       {
  180.         html << read;
  181.       }
  182.     }
  183.   }
  184.   html << "</TD>\n</TR>\n</TABLE>\n</HTML>";
  185.   csv.close();
  186.   html.close();
  187.   return 0; // Conversion was succesful
  188. }
  189.  
  190.  
  191. /*-checkhelp function---------------------------------------------------------
  192.  
  193.    Checks if help was called (--help parameter)
  194.  
  195.    Returns TRUE for yes, FALSE for no
  196. */
  197.  
  198. BOOL checkhelp(char* csvfile)
  199. {
  200.   if (strcmp(csvfile,"--help")==0)
  201.   {
  202.     /* Help asked for */
  203.     cout << "csv2html <csvfile> <htmloutputfile>\n\nVersion " << version <<"\n\nConverts a .CSV file to an HTML table. <csvfile> is the .csv file and <htmloutputfile> is the HTML file to write.\n";
  204.     return TRUE;
  205.   }
  206.   else
  207.   {
  208.     return FALSE;
  209.   }
  210. }
  211.  
  212. /*-checkversion function------------------------------------------------------
  213. */
  214.  
  215. /* Function as above but checks for --version instead */
  216.  
  217. BOOL checkversion(char* csvfile)
  218. {
  219.         if (strcmp(csvfile,"--version")==0)
  220.         {
  221.                 /* Version number requested */
  222.                 cout << "Version " << version <<".\n";
  223.                 return TRUE;
  224.         }
  225.         else
  226.         {
  227.                 return FALSE;
  228.         }
  229. }
  230.  
  231. /*-main function--------------------------------------------------------------
  232. */
  233.  
  234. /* main. Includes parameters of csv filename and html filename (outfile).
  235. */
  236.  
  237. void main(int argv,char * argc[])
  238. {
  239.  
  240.   /* Set up version string */
  241.  
  242.   strcpy(version,"1.0");
  243.  
  244.   /* argv = number of parameters 
  245.      argc = parameters
  246.   */
  247.  
  248.   int returned=0; /* Takes return value from process */
  249.  
  250.   if (argv>3) /* More than two parameters so display warning! */
  251.   {
  252.     cerr << "Warning : additional parameters ignored!\n";
  253.   }
  254.  
  255.   if (argv<3) /* Less than 3 parameters so error! */
  256.   {
  257.     if (argv==2) /* Check for help */
  258.     {
  259.       if (checkhelp(argc[1])==TRUE)
  260.       {
  261.         exit(0);
  262.       }
  263.       else if (checkversion(argc[1])==TRUE)
  264.       {
  265.         exit(0);
  266.       }
  267.       else
  268.       {
  269.         cerr << "csv2html: Misssing file name\nUse csv2html <csvfile> <htmloutputfile> or csv2html --help for help\n";
  270.         exit(0);
  271.       }
  272.     }
  273.     else
  274.     {
  275.       cerr << "csv2html: Misssing file name\nUse csv2html <csvfile> <htmloutputfile> or csv2html --help for help\n";
  276.       exit(0);
  277.     }  
  278.   }
  279.  
  280.   /* Parameters are correct and we have got file names so start processing! */
  281.  
  282.   returned=ProcessFiles(argc[1],argc[2]);
  283.   /* Check for status before quitting */
  284.   if (returned==0) /* OK */
  285.   {
  286.   }
  287.   if (returned==1) /* Invalid CSV File */
  288.   {
  289.     cerr << "Invalid csv file - check the file name exists!\n";
  290.   }
  291.   if (returned==2) /* Invalid HTML File */
  292.   {
  293.     cerr << "Cannot create output file - check if the name exists and that\nyou have write permission to the directory.\n";
  294.   }
  295. }
  296.